package demo4.controller;

import demo4.pojo.User;
import demo4.mapper.IUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component//
public class UserController {
    @Autowired//
    private IUserMapper userMapper;
    public void start()
    {
        System.out.println("请输入操作序号");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.注册");
        System.out.println("2.登录");
        System.out.println("3.修改密码");
        System.out.println("4.用户列表");
        int num = scanner.nextInt();
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            default:
                System.out.println("输入错误");
                break;
        }
    }
    private void regUser()
    {
        System.out.println("欢迎注册！！！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        if (userMapper.selectUserByUsername( username)!= null) {
            System.out.println("该用户已存在");
            return;
        }
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();

        User user = new User(0,username,password,nickname,age);
        int result = userMapper.insertUser(user);
        System.out.println(result>0?"注册成功":"注册失败");
    }
    private void loginUser()
    {
        System.out.println("登录!!!!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入您的密码：");
        String password = scanner.nextLine();
        User  user = userMapper.selectUserByUsername(username);
        if (user != null){
            if (user.getPassword().equals(password)){
                System.out.println("登录成功，欢迎【"+user.getNickname()+"】回家");
            }else{
                System.out.println("密码错误");
            }
        } else {
            System.out.println("用户不存在");
        }
    }
    private void listUser(){
        List<User> list = userMapper.selectAllUser();
        for (User user : list) {
            System.out.println(user);
        }
    }

    //修改密码
    private void updateUser(){
        System.out.println("修改密码!!!!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入旧的密码：");
        String oldPassword = scanner.nextLine();
        System.out.println("请输入新的密码：");
        String newPassword = scanner.nextLine();
        User  user = userMapper.selectUserByUsername(username);
        if (user != null){
            if ( user.getPassword().equals(oldPassword)){
                user.setPassword(newPassword);
                int count = userMapper.updateUser(user);
                System.out.println(count > 0 ? "修改成功！" : "修改失败！");
            } else {
                System.out.println("密码错误！");
            }
        } else {
            System.out.println("用户不存在！");
        }

    }
}
